<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\model\Formwork as FormworkModel;
use app\index\model\FormworkOrder as FormworkOrderModel;
use hema\Tree;
use think\Cache;
use addons\wxpaypos\library\WxPay;

class Formwork extends Frontend
{
	protected $model = null;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'index';
    protected $rulelist = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Link');
        // 必须将结果集转换为数组
        $link = collection($this->model->order('weigh desc,id desc')->select())->toArray();
        $this->view->assign('link', $link);
        
        $this->model = model('UserRule');
        // 必须将结果集转换为数组
        $ruleList = collection($this->model->where(['status' => 'normal','ismenu' => 1,'name' =>['like','index%']])->order('weigh desc,id desc')->select())->toArray();
        $this->rulelist = Tree::instance()->getMenuTree($ruleList);
        $this->view->assign('menudata', $this->rulelist);
    }



    public function index($order='default')
    {
        $model = new FormworkModel;
        $list = $model->getList();
        $this->view->assign('key', 'index/formwork/index');
        $this->view->assign('title', __('Formwork market'));
        $this->view->assign('order', $order);
        $this->view->assign('list',$list);
        return $this->view->fetch();
    }

    public function detail($id)
    { 
        $detail = FormworkModel::detail($id);
        $this->view->assign('key', 'index/formwork/index');
        $this->view->assign('title', $detail['title']);
        $this->view->assign('detail', $detail);
        return $this->view->fetch($detail['name']);
    }

    /**
     * 购买付款
     */
    public function buy($id,$shop_type=10)
    {
        $user = $this->auth->getUser();
        if(!$user){
            return $this->error('请先登录');
        }
        $model = FormworkModel::detail($id);
        //生成付款码
        $code_url = '';
        if($pay = get_addon_config('wxpaypos')){
            $pay['is_sub'] = 0; //不是特约商户
            $order_no = orderNo();
            $total_fee = $shop_type == 10 ? $model['single_price'] : $model['many_price'];
            $wxpay = new WxPay($pay);
            $code_url = $wxpay->unifiedorder($order_no,$total_fee,url('formwork/notify'),'','NATIVE');
            //存储订单信息
            Cache::set($order_no, [
                'user_id' => $user['id'],
                'total_fee' => $total_fee,
                'shop_type' => $shop_type,
                'order_no' => $order_no,
                'detail' => $model
            ]);
            $this->view->assign('code_url', $code_url);
        }
        $this->view->assign('code_url', $code_url);
        $this->view->assign('shop_type', $shop_type);
        $this->view->assign('key', 'index/formwork/index');
        $this->view->assign('title', '立即支付');
        $this->view->assign('model', $model);
        return $this->view->fetch();
    }

    /**
     * 站点扫码支付成功异步通知
     */
    public function notify()
    {
        $pay = get_addon_config('wxpaypos');
        $WxPay = new WxPay($pay);
        $WxPay->notify(new FormworkOrderModel,'add');
    }

}
